package com.fishingwithme.android.ui.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fishingwithme.android.data.local.PreferencesManager
import com.fishingwithme.android.utils.PermissionHelper
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

data class StartupUiState(
    val showLocationPermissionDialog: Boolean = false,
    val hasLocationPermission: Boolean = false,
    val hasShownPermissionDialog: Boolean = false
)

@HiltViewModel
class StartupViewModel @Inject constructor(
    private val preferencesManager: PreferencesManager,
    private val permissionHelper: PermissionHelper
) : ViewModel() {
    
    private val _uiState = MutableStateFlow(StartupUiState())
    val uiState: StateFlow<StartupUiState> = _uiState.asStateFlow()
    
    companion object {
        private const val KEY_HAS_SHOWN_LOCATION_DIALOG = "has_shown_location_permission_dialog"
    }
    
    init {
        checkInitialPermissionState()
    }
    
    private fun checkInitialPermissionState() {
        viewModelScope.launch {
            val hasLocationPermission = permissionHelper.hasLocationPermission()
            val hasShownDialog = preferencesManager.getBoolean(KEY_HAS_SHOWN_LOCATION_DIALOG, false).first()
            
            _uiState.value = _uiState.value.copy(
                hasLocationPermission = hasLocationPermission,
                hasShownPermissionDialog = hasShownDialog,
                showLocationPermissionDialog = !hasLocationPermission && !hasShownDialog
            )
        }
    }
    
    fun onLocationPermissionResult(isGranted: Boolean) {
        viewModelScope.launch {
            // Mark that we've shown the dialog
            preferencesManager.saveBoolean(KEY_HAS_SHOWN_LOCATION_DIALOG, true)
            
            _uiState.value = _uiState.value.copy(
                hasLocationPermission = isGranted,
                showLocationPermissionDialog = false,
                hasShownPermissionDialog = true
            )
        }
    }
    
    fun dismissLocationPermissionDialog() {
        viewModelScope.launch {
            // Mark that we've shown the dialog even if user dismissed it
            preferencesManager.saveBoolean(KEY_HAS_SHOWN_LOCATION_DIALOG, true)
            
            _uiState.value = _uiState.value.copy(
                showLocationPermissionDialog = false,
                hasShownPermissionDialog = true
            )
        }
    }
    
    fun checkLocationPermission() {
        val hasPermission = permissionHelper.hasLocationPermission()
        _uiState.value = _uiState.value.copy(hasLocationPermission = hasPermission)
    }
    
    // Method to reset the dialog state for testing purposes
    fun resetPermissionDialogState() {
        viewModelScope.launch {
            preferencesManager.saveBoolean(KEY_HAS_SHOWN_LOCATION_DIALOG, false)
            checkInitialPermissionState()
        }
    }
    
    // Development helper - force show dialog again
    fun showPermissionDialogForTesting() {
        _uiState.value = _uiState.value.copy(showLocationPermissionDialog = true)
    }
}